package com.acme.bankapp.domain.bank;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import com.acme.bankapp.exceptions.bank.NotEnoughFundsException;
import com.acme.bankapp.service.bank.BankService;
import static com.acme.bankapp.service.bank.BankReport.*;

public class BankApplication {

	public static void modifyBank(Bank bank, String nameOfClient,
			int numOfMinAccount, String depositOrWithdraw, int sum)
			throws NotEnoughFundsException, IllegalArgumentException {
		Client client = null;
		Set<Account> set = new TreeSet<>(new AccountComparator());

		HashSet<Client> clients = (HashSet<Client>) bank.getClients();
		for (Client cl : clients) {
			if (cl.getName().equals(nameOfClient)) {
				client = cl;
			}
		}
		if (depositOrWithdraw.equals("deposite")) {
			Account[] arrayAccount = (Account[]) client.getAccounts().toArray(
					new Account[client.getAccounts().size()]);
			arrayAccount[numOfMinAccount].deposit(sum);
			for (Account acc : arrayAccount) {
				set.add(acc);
			}
			client.setAccounts(set);
		}

		if (depositOrWithdraw.equals("withdraw")) {
			Account[] arrayAccount = (Account[]) client.getAccounts().toArray(
					new Account[client.getAccounts().size()]);
			arrayAccount[numOfMinAccount].withdraw(sum);
			for (Account acc : arrayAccount) {
				set.add(acc);
			}
			client.setAccounts(set);

		}

	}

	public static void main(String[] args) {
		Bank b = new Bank();
		Client client1 = new Client("Ivanov", Gender.MALE, 200, 10);
		Client client2 = new Client("Petrova", Gender.FEMALE, 250);
		Client client3 = new Client("Sidorov", Gender.MALE, 240, 50);
		BankService.addClient(b, client1);
		BankService.addClient(b, client2);
		BankService.addClient(b, client3);
		BankService.addAccount(b, "Ivanov", 100, 100);
		BankService.addAccount(b, "Ivanov", 1000, 100);
		BankService.printBank(b);
		try {
			modifyBank(b, "Ivanov", 0, "withdraw", 1050);
			modifyBank(b, "Sidorov", 0, "withdraw", 270);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NotEnoughFundsException e) {
			e.printStackTrace();
		}
		BankService.printBank(b);
		getNbOfCustomers(b);
		getNbOfAccounts(b);
		getAccountsSortedBySum(b);
		getBankCredit(b);
		getCustomerAccount(b);
	}
}
